package Interface;

import static util.Constant.STR_CONFIRM_ACTION;
import static util.Constant.STR_ERROR;
import static util.Constant.STR_EXCEPTION_SHOULD_HAVE_OCCURED;
import static util.Constant.STR_GO_BACK_MENU;
import static util.Constant.STR_PRESS_ENTER;
import static util.Constant.STR_PROGRAM_NAME;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

import lib.CommentManager;
import lib.EventManager;
import lib.ItemManager;
import lib.ItemlistManager;
import lib.TagManager;
import lib.UserManager;
import base.Comment;
import base.Event;
import base.Itemlist;
import base.Tag;
import base.User;
import base.items.Item;
import base.items.Mp3;
import base.items.Music;
import base.items.Photo;
import base.items.Text;
import base.items.Video;

/**
 * The Interface {@code class}. Create a console interface.
 * 
 * @author Peter NGO, Guillaume POUSSEL and Mathieu RUL
 * 
 * @version 1.0
 */
public class Interface {

    /**
     * The interface for adding a friend.
     */
    private static void addFriend() {
        Scanner in = new Scanner(System.in);
        String input;
        User user = UserManager.getInstance().getCurrentUser();
        User friend;
        try {
            Console.displayHeader("Add friend");
            Console.line();
            System.out.println("Enter the login to add to your friendlist:");

            input = Console.askText();
            friend = UserManager.getInstance().get(input);
            System.out.println(friend.profile());

            if (null != friend) {
                if (UserManager.getInstance().getCurrentUser().equals(friend)) {
                    System.err.println("You cannot became your friend !");
                }
                UserManager.getInstance().makeFriends(user, friend);
                System.out.println("Friend added! You are now friends with "
                        + friend.getFirstname() + " " + friend.getLastname() + ".");
            } else {
                System.err.println("This login does not exist!");
            }
            System.out.println(STR_PRESS_ENTER);
            input = in.nextLine();
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    private static void addTag(Event currentEvent) throws Exception {
        System.out.println("Tag name:");
        String tagName = Console.askText();
        System.out.println("Searching...");
        Tag[] tagsCherches = TagManager.getInstance().searchByName(tagName);
        if (tagsCherches.length == 0) {
            System.out.println("No tag found");
            System.out.println("Create a new tag? (Y)es / (N)o");

            String input = Console.askText();

            if (input.equalsIgnoreCase("y")) {
                Tag created = TagManager.getInstance().create(tagName);
                if (!EventManager.getInstance().addTag(currentEvent, created)) {
                    System.err.println(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
                } else {
                    System.out.println("Tag added and associated");
                }
            } else {
                return;
            }
        } else {
            System.out.println("Available tags :");
            for (Tag tag : tagsCherches) {
                System.out.println(tag);
            }
            System.out.println("Create a new tag? (Y)es / (N)o");

            String input = Console.askText();

            if (input.equalsIgnoreCase("y")) {
                Tag newTag = createTag();
                if (EventManager.getInstance().addTag(currentEvent, newTag)) {
                    System.out.println("Tag associated");
                } else {
                    System.err.println("Associating failed");
                }
            } else {
                System.out.println("Tag ID:");
                Integer tagID = Console.askInt();
                if (EventManager.getInstance().addTag(currentEvent.getID(), tagID)) {
                    System.out.println("Tag associated");
                } else {
                    System.err.println("Associating failed");
                }
            }
        }

    }

    private static void addTag(Item item) throws Exception {
        System.out.println("Tag name:");
        String tagName = Console.askText();
        System.out.println("Searching...");
        Tag[] tagsCherches = TagManager.getInstance().searchByName(tagName);
        if (tagsCherches.length == 0) {
            System.out.println("No tag found");
            System.out.println("Create a new tag? (Y)es / (N)o");

            String input = Console.askText();

            if (input.equalsIgnoreCase("y")) {
                Tag created = TagManager.getInstance().create(tagName);
                if (!ItemManager.getInstance().addTag(item, created)) {
                    System.err.println(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
                } else {
                    System.out.println("Tag added and associated");
                }
            } else {
                return;
            }
        } else {
            System.out.println("Available tags :");
            for (Tag tag : tagsCherches) {
                System.out.println(tag);
            }
            System.out.println("Create a new tag? (Y)es / (N)o");

            String input = Console.askText();

            if (input.equalsIgnoreCase("y")) {
                Tag newTag = createTag();
                if (ItemManager.getInstance().addTag(item, newTag)) {
                    System.out.println("Tag associated");
                } else {
                    System.err.println("Associating failed");
                }
            } else {
                System.out.println("Tag ID:");
                Integer tagID = Console.askInt();
                if (ItemManager.getInstance().addTag(item.getID(), tagID)) {
                    System.out.println("Tag associated");
                } else {
                    System.err.println("Associating failed");
                }
            }
        }
    }

    private static Comment createComment(Item item) {
        System.out.println("Comment content:");
        String content = Console.askText();

        Comment comm = CommentManager.getInstance().create(content,
                UserManager.getInstance().getCurrentUser());
        item.addComment(comm);
        ItemManager.getInstance().save();
        return comm;
    }

    private static void createEvent() {
        String name = null;
        String input = "";
        boolean inputOk = false;

        while (!inputOk) {
            Console.displayHeader("Event creation");
            Console.line();

            System.out.println("Name: ");

            name = Console.askText();

            System.out.println(STR_CONFIRM_ACTION);
            input = Console.askText();
            if (input.equalsIgnoreCase("c")) {
                inputOk = true;
            } else if (input.equalsIgnoreCase("b")) {
                System.out.println("Going back to the User menu...");
                try {
                    manageEvents();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("Event " + EventManager.getInstance().create(name)
                + " created successfully!");

        Console.pause();

    }

    /**
     * The interface for creating an Item.
     */
    private static void createItem(Itemlist itemlist) throws Exception {
        boolean inputOk = false;

        while (!inputOk) {
            System.out.println("Type:");
            String type = Console.askText();
            Item item;
            try {
                item = ItemManager.getInstance().create(type);
            } catch (Exception e) {
                System.err.println("Unable to instatiate an object with the type `" + type + "`");
                return;
            }

            System.out.println("Path (leave blank if not):");
            String path = Console.askText(true);
            if (!path.trim().isEmpty())
                item.setPath(path);
            if (null != item.getPath())
                System.out.println("Path set");

            System.out.println("Title"
                    + (item.getTitle() == null ? " (default " + item.getTitle() + ")" : "") + ":");
            String title = Console.askText(true);
            if (!title.trim().isEmpty())
                item.setTitle(title);

            itemlist.add(item.getID());
            ItemManager.getInstance().save();
            ItemlistManager.getInstance().save();

            if (item.getType().equals("Photo")) {
                System.out.println("Location (you could leave blank):");
                String location = Console.askText(true);
                ((Photo) item).setLocation(location);
            } else if (item.getType().equals("Music") || item.getType().equals("Mp3")) {
                System.out.println("Artist (you could leave blank):");
                String artist = Console.askText(true);
                ((Music) item).setArtist(artist);
            } else if (item.getType().equals("Author")) {
                System.out.println("Author (you could leave blank):");
                String author = Console.askText(true);
                ((Text) item).setAuthor(author);
            } else if (item.getType().equals("Video")) {
                System.out.println("Director (you could leave blank):");
                String director = Console.askText(true);
                ((Video) item).setDirector(director);
            } else if (item.getType().equals("Mp3")) {
                System.out.println("Bitrate (you could leave blank):");
                String bitrate = Console.askText(true);
                ((Mp3) item).setBitrate(Integer.valueOf(bitrate));
            }
            ItemManager.getInstance().save();
            inputOk = true;

        }
    }

    /**
     * The interface for create an ItemList.
     */
    private static void createItemList() {
        String name = null;
        String input = "";
        boolean inputOk = false;

        while (!inputOk) {
            Console.displayHeader("Itemlist creation");
            Console.line();

            System.out.println("Name: ");

            name = Console.askText();

            System.out.println(STR_CONFIRM_ACTION);
            input = Console.askText();
            if (input.equalsIgnoreCase("c")) {
                inputOk = true;
            } else if (input.equalsIgnoreCase("b")) {
                System.out.println("Going back to the Itemlist menu...");
                try {
                    manageItemLists();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        ItemlistManager itemlistManager = ItemlistManager.getInstance();
        System.out.println("Itemlist " + itemlistManager.create(name) + " created successfully!");

        Console.pause();
    }

    private static Tag createTag() {
        System.out.println("New tag name:");
        String tagName = Console.askText();

        Tag existeDeja = TagManager.getInstance().get(tagName);
        return existeDeja == null ? TagManager.getInstance().create(tagName) : existeDeja;
    }

    /**
     * The interface for get a password.
     * 
     * @return a String representing the password.
     */
    private static String getPassword() {
        String input1, input2;
        do {
            System.out.println("Password: ");
            input1 = Console.askText();
            System.out.println("Confirm Password: ");
            input2 = Console.askText();
            if (!input1.equals(input2)) {
                System.err.println("Confirmation failed, please try again.");
            }
        } while (!input1.equals(input2));
        return input1;
    }

    private static void listComments(Item item) {
        Console.displayHeader("Comments list");
        Console.line();
        HashSet<Integer> commentsID = item.getComments();
        if (commentsID.isEmpty()) {
            System.out.println("No comment !");
            return;
        }
        for (Integer cID : item.getComments()) {
            System.out.println("\t*" + CommentManager.getInstance().get(cID));
        }
        Console.line();
        Console.pause();
    }

    private static void listEvents() {
        try {
            Console.displayHeader("Events");
            Console.line();

            Set<Event> events = EventManager.getInstance().getAll();
            if (events.isEmpty()) {
                System.out.println("Nothing to list");
            } else {
                for (Event event : events) {
                    Console.line();
                    System.out.println(event);
                }
            }
            Console.line();
            Console.pause();
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    /**
     * The interface for list friends of the current user.
     */
    private static void listFriends() {
        try {
            User user = UserManager.getInstance().getCurrentUser();
            HashSet<Integer> friendlist = user.getFriends();
            Console.displayHeader("Friends list");
            Console.line();
            if (friendlist.isEmpty()) {
                System.err.println("\tYou have no friends...");
            } else {
                for (Integer friendId : friendlist) {
                    System.out.println(UserManager.getInstance().get(friendId).profile());
                }
            }
            Console.line();
            Console.pause();
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    private static void listFriendsItems() {
        String input;
        HashSet<Integer> itemlistsIDs;
        User user = UserManager.getInstance().getCurrentUser();
        User friend;

        try {
            Console.displayHeader("View friend's item");
            Console.line();
            System.out.println("Enter the login of the friend:");

            input = Console.askText();
            friend = UserManager.getInstance().get(input);
            if (friend == null) {
                System.err.println("This login does not exist!");
            } else if (UserManager.getInstance().isFriends(user, friend)) {
                itemlistsIDs = friend.getItemLists();
                if (itemlistsIDs.isEmpty())
                    System.out.println("No itemlist");
                // pour chaque itemlist de l'utilisateur
                for (Integer itemlistID : itemlistsIDs) {
                    // on cherche les itemIDs
                    HashSet<Integer> itemIDs = ItemlistManager.getInstance().get(itemlistID)
                            .getItems();
                    if (itemIDs.isEmpty())
                        System.out.println("No items");
                    // pour chque itemID trouve on affiche l'item associe
                    for (Integer itemID : itemIDs) {
                        System.out.println(ItemManager.getInstance().get(itemID));
                    }
                }
            } else {
                System.err.println("This person isn't your friend!");
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        Console.pause();
    }

    /**
     * The interface for list Itemlists of the current user.
     */
    private static void listItemLists() {
        try {
            Console.displayHeader("Itemlists");
            Console.line();

            User user = UserManager.getInstance().getCurrentUser();
            HashSet<Integer> itemlists = user.getItemLists();
            ItemlistManager itemlistManager = ItemlistManager.getInstance();
            if (itemlists.isEmpty()) {
                System.out.println("Nothing to list");
            } else {
                for (Integer ID : itemlists) {
                    Itemlist itemlist = itemlistManager.get(ID);
                    System.out.println("ID: " + itemlist.getID() + "\nName: " + itemlist.getName());
                }
            }

            Console.line();
            Console.pause();
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    private static void listTags(Event currentEvent) {
        Console.displayHeader("Tags");
        Console.line();
        System.out.println("Tags of Event " + currentEvent.getName());

        TagManager tagManager = TagManager.getInstance();
        HashSet<Integer> tagsList = currentEvent.getTags();
        for (Integer ID : tagsList) {
            System.out.println(tagManager.get(ID).toString());
        }

        Console.line();
        Console.pause();
    }

    /**
     * The interface for list tags of an Item.
     */
    private static void listTags(Item item) {
        Console.displayHeader("Tags");
        Console.line();
        System.out.println("Tags of Item " + item.getTitle());

        TagManager tagManager = TagManager.getInstance();
        HashSet<Integer> tagsList = item.getTags();
        for (Integer ID : tagsList) {
            System.out.println(tagManager.get(ID).toString());
        }

        Console.line();
        Console.pause();
    }

    /**
     * The interface for list all the user.
     * 
     * @throws Exception
     */
    private static void listUsers() throws Exception {
        Console.displayHeader("Users");
        Console.line();
        System.out.print(UserManager.getInstance().toString());
        Console.line();
        Console.pause();
        mainMenu();
    }

    /**
     * The interface for login.
     */
    private static void login() throws Exception {
        String login = "", password = "";
        User user = null;

        while (user == null) {
            Console.displayHeader("Login");
            Console.line();
            System.out.println("Login: ");
            login = Console.askText();

            System.out.println("Password: ");
            password = Console.askText();
            UserManager.getInstance().connect(login, password);
            user = UserManager.getInstance().getCurrentUser();

            if (null != user) {
                System.out.println("Login successful.");
                System.out.println("Hello " + user.getFirstname() + "!");
                userMenu();
            }

            // sinon c'est l'erreur, et on propose de recommencer ou de sortir
            else {
                System.err.println("Login failed. ");
                if (!Console.tryAgain()) {
                    System.out.println(STR_GO_BACK_MENU);
                    mainMenu();
                    return;
                }
            }
        }
    }

    /**
     * The main program function.
     * 
     * @param args The string arguments.
     */
    public static void main(String[] args) {
        Console.displayHeader("Welcome on " + STR_PROGRAM_NAME);
        try {
            mainMenu();
        } catch (Exception e) {
            System.err.println(STR_EXCEPTION_SHOULD_HAVE_OCCURED);
        }
        System.exit(0);
    }

    /**
     * The main menu of the programm allow to login or register an account.
     * 
     * @throws Exception
     */
    private static void mainMenu() throws Exception {
        Console.displayHeader("Main Menu");
        Console.displayMenuWithFirstLetters(new String[] { "Login", "Register", "View all users",
                "Exit" });

        String input = Console.askText();
        if (input.equalsIgnoreCase("l")) {
            login();
        } else if (input.equalsIgnoreCase("r")) {
            register();
        } else if (input.equalsIgnoreCase("v")) {
            listUsers();
        } else if (input.equalsIgnoreCase("e")) {
            System.out.println(STR_PROGRAM_NAME + " will exit now...Bye bye");
            System.exit(0);
        } else {
            System.err.println(STR_ERROR);
        }
    }

    private static void manageEvents() throws Exception {
        int input;
        boolean inputOk = false;
        while (!inputOk) {
            Console.displayHeader("Events management");
            Console.displayMenu(new String[] { "List events", "Create new event",
                    "Update an event", "Delete an event", "Back to user menu" });
            input = Console.askInt();
            if (input == 1) {
                listEvents();
            } else if (input == 2) {
                createEvent();
            } else if (input == 3) {
                updateEvent();
            } else if (input == 4) {
                removeEvent();
            } else if (input == 5) {
                System.out.println(STR_GO_BACK_MENU);
                return;
            } else {
                System.err.println(STR_ERROR);
            }
        }
    }

    private static void manageFriends() {
        int input;
        boolean inputOk = false;

        while (!inputOk) {
            Console.displayHeader("Friends management");
            Console.displayMenu(new String[] { "List my friends", "Add new friend", "Read friend",
                    "Delete friend", "Back to user menu" });
            input = Console.askInt();

            if (input == 1) {
                listFriends();
            } else if (input == 2) {
                addFriend();
            } else if (input == 3) {
                readFriend();
            } else if (input == 4) {
                removeFriend();
            } else if (input == 5) {
                System.out.println(STR_GO_BACK_MENU);
                return;
            } else {
                System.err.println(STR_ERROR);
            }
        }
    }

    private static void manageItemLists() throws Exception {
        int input;
        boolean inputOk = false;
        while (!inputOk) {
            Console.displayHeader("Itemlists management");
            Console
                    .displayMenu(new String[] { "List my itemLists", "Create new itemList",
                            "Read itemList", "Manage Items in list", "Delete itemList",
                            "Back to user menu" });
            input = Console.askInt();
            if (input == 1) {
                listItemLists();
            } else if (input == 2) {
                createItemList();
            } else if (input == 3) {
                readItemList();
            } else if (input == 4) {
                updateItemList();
            } else if (input == 5) {
                removeItemList();
            } else if (input == 6) {
                System.out.println(STR_GO_BACK_MENU);
                return;
            } else {
                System.err.println(STR_ERROR);
            }
        }
    }

    private static void readFriend() {
        String input;
        User friend;
        try {
            Console.displayHeader("Read friend");
            Console.line();
            System.out.println("Enter the login of the friend to read:");

            input = Console.askText();
            friend = UserManager.getInstance().get(input);
            if (null != friend) {
                System.out.println(friend);
            } else {
                System.err.println("This login does not exist!");
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        Console.pause();
    }

    private static void readItem(Itemlist itemlist) throws Exception {
        int input;
        Item item;

        System.out.println("Enter the item ID you would like to read:");

        input = Console.askInt();

        if (!itemlist.contains(input)) {
            System.err.println("This itemlist doesn't contain this item");
            return;
        }

        item = ItemManager.getInstance().get(input);
        if (item == null) {
            System.out.println("No item " + input);

        } else {
            int choice;
            do {
                System.out.println(item);
                Console.displayMenu(new String[] { "Change title", "List tags", "Add a tag",
                        "Remove a tag", "View comments", "Add comment", "Back to itemlist menu" });
                choice = Console.askInt();

                if (choice == 1) {
                    System.out.println("New title:");

                    item.setTitle(Console.askText());
                    System.out.println("Title changed successfullly");
                    ItemlistManager.getInstance().save();
                } else if (choice == 2) {
                    listTags(item);
                } else if (choice == 3) {
                    addTag(item);
                } else if (choice == 4) {
                    removeTag(item);
                } else if (choice == 5) {
                    listComments(item);
                } else if (choice == 6) {
                    createComment(item);
                } else if (choice == 7) {
                    return;
                } else {
                    System.err.println(STR_ERROR);
                }
            } while (choice > 0 && choice < 7);
        }

    }

    private static void readItemList() {
        Integer id;
        Itemlist itemlist;

        Console.displayHeader("ItemList Read");
        Console.line();

        System.out.println("Enter the ID of the itemlist to read: ");
        id = Console.askInt();
        itemlist = ItemlistManager.getInstance().get(id);

        if (UserManager.getInstance().isOwnedItemlist(id)
                || UserManager.getInstance().isFriendItemlist(id)) {
            System.out.println(itemlist);
            Console.line();
        } else {
            System.err.println("This itemlist isn't accessible to you.");
        }

        Console.pause();
    }

    private static void register() {
        try {
            String login = "", password = "", firstname = "", lastname = "";
            String input = "";

            boolean registerOk = false;
            while (!registerOk) {
                Console.displayHeader("Register new user");
                System.out.println("======================================");

                System.out.println("Login: ");
                login = Console.askText();

                password = getPassword();

                System.out.println("First name: ");
                firstname = Console.askText();

                System.out.println("Last name: ");
                lastname = Console.askText();

                System.out.println("\nLogin: " + login + "\nFirst name: " + firstname
                        + "\nLast name: " + lastname);
                System.out.println(STR_CONFIRM_ACTION);

                input = Console.askText();
                if (input.equalsIgnoreCase("c")) {
                    registerOk = true;
                } else if (input.equalsIgnoreCase("b")) {
                    System.out.println(STR_GO_BACK_MENU);
                    mainMenu();
                }
            }

            UserManager.getInstance().create(login, password, lastname, firstname);
            System.out.println("User created successfully !");
            Console.pause();
            mainMenu();
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void removeEvent() {
        Integer id;
        Event event;

        Console.displayHeader("Event delete");
        Console.line();

        System.out.println("Enter the ID of the event to delete: ");

        id = Console.askInt();
        event = EventManager.getInstance().get(id);

        if (null != event) {
            System.out.println(event);
            if (Console.confirm()) {
                try {
                    EventManager.getInstance().remove(id);
                    System.out.println("Event deleted!");
                } catch (Exception e) {
                    System.err.println("Delete failed.");
                    e.printStackTrace();
                }

            } else {
                System.out.println("Canceled...");
            }
            Console.line();
        } else {
            System.out.println("This event does not exists.");
        }

        Console.pause();

    }

    /**
     * The interface for removing a friend.
     */
    private static void removeFriend() {
        String input;
        User user = UserManager.getInstance().getCurrentUser();
        User friend;
        try {
            Console.displayHeader("Remove friend");
            Console.line();
            System.out.println("Enter the login of the friend to delete:");
            input = Console.askText();
            friend = UserManager.getInstance().get(input);
            if (friend == null) {
                System.err.println("This login does not exist!");
            } else if (UserManager.getInstance().isFriends(user, friend)) {
                UserManager.getInstance().removeFriends(user, friend);
                System.out.println("Friend deleted! You aren't friends with "
                        + friend.getFirstname() + " " + friend.getLastname() + " anymore.");
            } else {
                System.err.println("This person isn't your friend anyways!");
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        Console.pause();
    }

    /**
     * The interface for removing an Item.
     */
    private static void removeItem(Itemlist itemlist) {
        int itemId;
        Item item;

        System.out.println("Enter the item ID you would like to delete:");
        itemId = Console.askInt();
        item = ItemManager.getInstance().get(itemId);

        System.out.println(item);
        if (Console.confirm()) {
            try {
                if (itemlist.remove(itemId)) {
                    ItemManager.getInstance().remove(itemId);
                    ItemlistManager.getInstance().save();
                    System.out.println("Item deleted!");
                } else {
                    System.err.println("Item delete failed!");
                }
            } catch (Exception e) {
                System.err.println("Item delete failed!");
                e.printStackTrace();
            }

        } else {
            System.out.println("Cancelled...");
        }
    }

    /**
     * The interface for removing an Itemlist.
     */
    private static void removeItemList() {
        Integer id;
        User user = UserManager.getInstance().getCurrentUser();
        Itemlist itemlist;

        Console.displayHeader("ItemList delete");
        Console.line();

        System.out.println("Enter the ID of the itemlist to delete: ");

        id = Console.askInt();
        itemlist = ItemlistManager.getInstance().get(id);

        if (null != itemlist && UserManager.getInstance().isOwnedItemlist(id)) {
            System.out.println(itemlist);
            if (Console.confirm()) {
                try {
                    if (user.removeItemList(id)) {
                        ItemlistManager.getInstance().remove(id);
                    }
                    System.out.println("Item list deleted!");
                } catch (Exception e) {
                    System.err.println("Delete failed.");
                    e.printStackTrace();
                } finally {
                    UserManager.getInstance().save();
                }

            } else {
                System.out.println("Canceled...");
            }
            Console.line();
        } else {
            System.out.println("This itemlist isn't accessible to you.");
        }

        Console.pause();
    }

    private static void removeTag(Event currentEvent) {
        for (Integer id : currentEvent.getTags()) {
            System.out.println(TagManager.getInstance().get(id));
        }
        System.out.println("Tag ID:");
        int tagID = Console.askInt();
        if (EventManager.getInstance().removeTag(currentEvent.getID(), tagID)) {
            System.out.println("Tag deleted successfully");
        } else {
            System.err.println("Tag delete failed");
        }
    }

    private static void removeTag(Item item) {
        for (Integer id : item.getTags()) {
            System.out.println(TagManager.getInstance().get(id));
        }
        System.out.println("Tag ID:");
        int tagID = Console.askInt();
        if (ItemManager.getInstance().removeTag(item.getID(), tagID)) {
            System.out.println("Tag deleted successfully");
        } else {
            System.err.println("Tag delete failed");
        }
    }

    private static void updateEvent() throws Exception {
        int input;
        int id;
        Event currentEvent;

        Console.displayHeader("Event update");
        Console.line();

        System.out.println("Enter the ID of the event to update: ");
        id = Console.askInt();
        currentEvent = EventManager.getInstance().get(id);
        if (null != currentEvent) {
            do {
                System.out.println(currentEvent);
                Console.displayMenu(new String[] { "Change name", "List tags", "Add new tag",
                        "Remove tag", "Back to event menu" });

                input = Console.askInt();

                if (input == 1) {
                    System.out.println("New name:");
                    currentEvent.setName(Console.askText());
                    System.out.println("Name change succesful:");
                    EventManager.getInstance().save();
                } else if (input == 2) {
                    listTags(currentEvent);
                } else if (input == 3) {
                    addTag(currentEvent);
                } else if (input == 4) {
                    removeTag(currentEvent);
                } else if (input == 5) {
                    return;
                } else {
                    System.err.println(STR_ERROR);
                }
            } while (input > 0 && input < 5);
        } else {
            System.err.println("This event does not exists.");
        }
        Console.pause();
    }

    private static void updateItemList() throws Exception {
        int input;
        int id;
        Itemlist currentItemList;

        Console.displayHeader("ItemList update");
        Console.line();

        System.out.println("Enter the ID of the itemlist to update: ");
        id = Console.askInt();
        currentItemList = ItemlistManager.getInstance().get(id);

        if (UserManager.getInstance().isOwnedItemlist(id)) {
            do {
                System.out.println(currentItemList);
                Console.displayMenu(new String[] { "Change name", "Add new item", "Read item",
                        "Delete item", "Back to itemlist menu" });

                input = Console.askInt();

                if (input == 1) {
                    System.out.println("New name:");
                    currentItemList.setName(Console.askText());
                    System.out.println("Name change succesful:");
                    ItemlistManager.getInstance().save();
                } else if (input == 2) {
                    createItem(currentItemList);
                } else if (input == 3) {
                    readItem(currentItemList);
                } else if (input == 4) {
                    removeItem(currentItemList);
                } else if (input == 5) {
                    return;
                } else {
                    System.err.println(STR_ERROR);
                }
            } while (input > 0 && input < 5);
        } else {
            System.err.println("This itemlist isn't accessible to you.");
        }
        Console.pause();
    }

    private static void userChangeFirstname() {
        System.out.println("New first name: ");
        String firstname = Console.askText();
        UserManager.getInstance().getCurrentUser().setFirstname(firstname);
        UserManager.getInstance().save();
        System.out.println("First name changed successfully !");
        Console.pause();
    }

    private static void userChangeLastname() {
        System.out.println("New last name: ");
        String lastname = Console.askText();
        UserManager.getInstance().getCurrentUser().setLastname(lastname);
        UserManager.getInstance().save();
        System.out.println("Last name changed successfully !");
        Console.pause();
    }

    private static void userChangePassword() {
        String password = getPassword();
        UserManager.getInstance().getCurrentUser().setPassword(password);
        UserManager.getInstance().save();
        System.out.println("Password changed successfully !");
        Console.pause();
    }

    private static void userEditMenu() throws Exception {
        int input;
        do {
            Console.displayHeader("User Menu");
            Console.displayMenu(new String[] { "Change firstname", "Change lastname",
                    "Change password", "Go back to user menu" });

            input = Console.askInt();

            if (input == 1) {
                userChangeFirstname();
            } else if (input == 2) {
                userChangeLastname();
            } else if (input == 3) {
                userChangePassword();
            } else if (input == 4) {
                userMenu();
            } else {
                System.err.println(STR_ERROR);
            }
        } while (input > 0 && input < 4);
    }

    private static void userMenu() throws Exception {
        int input;
        do {
            Console.displayHeader("User Menu");
            Console.displayMenu(new String[] { "Manage itemLists", "Manage events",
                    "Manage friends", "View friend items", "View profile", "Edit profile",
                    "Log off" });

            input = Console.askInt();

            if (input == 1) {
                manageItemLists();
            } else if (input == 2) {
                manageEvents();
            } else if (input == 3) {
                manageFriends();
            } else if (input == 4) {
                listFriendsItems();
            } else if (input == 5) {
                viewProfile();
            } else if (input == 6) {
                userEditMenu();
            } else if (input == 7) {
                System.out.println("Logging off...");
                UserManager.getInstance().disconnect();
                return;
            } else {
                System.err.println(STR_ERROR);
            }
        } while (input > 0 && input < 6);
    }

    private static void viewProfile() {
        System.out.println("Here, you can see your profile:");
        System.out.println(UserManager.getInstance().getCurrentUser());

        Console.pause();

    }
}